package com.kangaroo.myhome.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.kangaroo.myhome.mapper.TInspProjectPlanMapper;
import com.kangaroo.myhome.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kangaroo.myhome.mapper.TInspProjectMapper;
import com.kangaroo.myhome.utils.SecurityUtils;
import com.kangaroo.myhome.vo.ResponseModel;

import tk.mybatis.mapper.util.StringUtil;

@Service
public class TInspProjectPlanService {

    @Autowired
    private TInspProjectPlanMapper tInspProjectPlanMapper;

    @Autowired
    private TInspProjectMapper tInspProjectMapper;

    /**
     * @param projectId
     * @param page
     * @param rows
     * @return
     */
    public ResponseModel findAll(String projectId, int page,
        int rows) {
        ResponseModel model = new ResponseModel();

        CommonSearch user = new CommonSearch();
        user.setString1(projectId);
        user.setPagination(page, rows);

        List<TInspProjectPlan> users = tInspProjectPlanMapper.selectByPagination(user);
        long count = tInspProjectPlanMapper.selectByPaginationCount(user);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }

    /**
     * @param subway
     * @return
     */
    /*public ResponseModel findReportData(String subway) {
        ResponseModel model = new ResponseModel();

        CommonSearch search = new CommonSearch();
        search.setString1(subway);

        List<Map> tInspProjects = tInspProjectPlanMapper.findReportData(search);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setData(tInspProjects);
        return model;
    }*/

    /*public ResponseModel findAllJson(String subway, String cat, String degree, String stage, String name, int page,
        int rows) {
        ResponseModel model = new ResponseModel();

        CommonSearch user = new CommonSearch();
        user.setString1(subway);
        user.setString2(cat);
        user.setString3(degree);
        user.setString4(stage);
        user.setString5(name);
        user.setPagination(page, rows);

        List<TInspProject> users = tInspProjectPlanMapper.selectByPaginationJson(user);
        long count = tInspProjectPlanMapper.selectByPaginationJsonCount(user);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }*/

    /*public List<TInspProject> findAllExcel(String subway, String cat, String degree, String stage) {
        ResponseModel model = new ResponseModel();

        CommonSearch user = new CommonSearch();
        user.setString1(subway);
        user.setString2(cat);
        user.setString3(degree);
        user.setString4(stage);
        user.setPagination(1, 999999);

        List<TInspProject> users = tInspProjectPlanMapper.findAllExcel(user);
        return users;
    }*/

    /**
     * @param tInspProjectPlan
     * @return TInspProjectPlan tInspProjectPlan
     */
    public ResponseModel edit(TInspProjectPlan tInspProjectPlan) {

        ResponseModel model = new ResponseModel();

        if (tInspProjectPlan == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }
        if(tInspProjectPlan.getProjectId()==null || "".equals(tInspProjectPlan.getProjectId())){
            model.setCode(201);
            model.setMessage("项目不存在，无法操作方案");
            return model;
        }


        int result = 0;
        String id = tInspProjectPlan.getId();
        if (StringUtil.isEmpty(id)) {
            tInspProjectPlan.setCreateTime(new Date());
            tInspProjectPlan.setId(UUID.randomUUID().toString());
            tInspProjectPlan.setCreator(SecurityUtils.getUsername());
            result = tInspProjectPlanMapper.insertSelective(tInspProjectPlan);
            TInspProject project = new TInspProject();
            project.setId(tInspProjectPlan.getProjectId());
            project.setStageTdcr(tInspProjectPlan.getStageTdcr());
            project.setStagePlan(tInspProjectPlan.getStagePlan());
            project.setStageDesign(tInspProjectPlan.getStageDesign());
            project.setStageCons(tInspProjectPlan.getStageCons());
            project.setStageTracing(tInspProjectPlan.getStageTracing());
            tInspProjectMapper.updateByPrimaryKeySelective(project);

        } else {
            tInspProjectPlan.setReviseTime(new Date());
            tInspProjectPlan.setId(UUID.randomUUID().toString());
            tInspProjectPlan.setRevisor(SecurityUtils.getUsername());
            result = tInspProjectPlanMapper.updateByPrimaryKeySelective(tInspProjectPlan);
        }

        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }

    public ResponseModel editInfo(TInspProjectPlan tInspProjectPlan) {

        ResponseModel model = new ResponseModel();

        if (tInspProjectPlan == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }

        int result = 0;
        String id = tInspProjectPlan.getId();
        if (StringUtil.isEmpty(id)) {
            model.setCode(201);
            model.setMessage("ID为空不可编辑");
            return model;
        } else {
            // 编辑的阶段
            String stage = tInspProjectPlan.getStage();
            tInspProjectPlan.setStage(null);
            TInspProject project = new TInspProject();
            project.setId(tInspProjectPlan.getId());
            if ("stageTdcr".equals(stage)) {
                project.setStageTdcr(tInspProjectPlan.getStageTdcr());
            } else if ("stagePlan".equals(stage)) {
                project.setStagePlan(tInspProjectPlan.getStagePlan());
            } else if ("stageDesign".equals(stage)) {
                project.setStageDesign(tInspProjectPlan.getStageDesign());
            } else if ("stageCons".equals(stage)) {
                project.setStageCons(tInspProjectPlan.getStageCons());
            } else if ("stageTracing".equals(stage)) {
                project.setStageTracing(tInspProjectPlan.getStageTracing());
            }
            result = tInspProjectPlanMapper.updateByPrimaryKeySelective(tInspProjectPlan);

        }

        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }

    /**
     * @param tInspProjectPlan
     * @return
     */
    public TInspProjectPlan view(TInspProjectPlan tInspProjectPlan) {
        ResponseModel model = new ResponseModel();

        TInspProjectPlanExample example = new TInspProjectPlanExample();
        String id = tInspProjectPlan.getId();
        String code = tInspProjectPlan.getName();
        TInspProjectPlan resultSubWay = null;
        if (StringUtil.isNotEmpty(id)) {
            resultSubWay = tInspProjectPlanMapper.selectByPrimaryKey(id);
            return resultSubWay;
        }
        if (StringUtil.isNotEmpty(code)) {
            example.createCriteria().andNameEqualTo(code);
            List<TInspProjectPlan> users = tInspProjectPlanMapper.selectByExample(example);
            if (users != null && users.size() > 0) {
                resultSubWay = users.get(0);
            }
        }

        return resultSubWay;
    }

    public void delete(TInspProjectPlan tInspProjectPlan) {
        String ids = tInspProjectPlan.getId();
        String[] idArr = ids.split(",");
        for (String id : idArr) {
            tInspProjectPlanMapper.deleteByPrimaryKey(id);
        }
    }

}
